package com.company.project.common.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.company.project.search.entity.Search;
import com.company.project.search.service.SearchEsService;
import com.company.project.search.service.SearchService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledTask {
    @Autowired
    private SearchService searchService;
    @Autowired
    private SearchEsService searchEsService;

    /**
     * 定时任务 添加到es数据库中 每天凌晨2点进行
     */
    @Scheduled(cron = " 0 0 2 * * ?")
    //@Scheduled(cron = "*/60 * * * * ?")
    public void saveSearch() {
        System.out.println("执行定时任务");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);

        Date date = calendar.getTime();

        LambdaQueryWrapper<Search> query = new LambdaQueryWrapper<>();
        query.ge(Search::getCreateTime, date);
        List<Search> list = searchService.list(query);
        searchEsService.saveAll(list);

    }
}
